package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassNameOrderController
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/16 下午8:23
 * @Version 1.0
 **/
@RestController("adminOrderController")
@Slf4j
@Api(tags = "订单管理")
@RequestMapping("/admin/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 条件查询订单
     * @param ordersPageQueryDTO 查询条件
     * @return Result<PageResult> 查询结果
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("条件查询订单")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("条件查询订单,{}" , ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }


    @GetMapping("/statistics")
    @ApiOperation("订单统计")
    //TODO 想要开启缓存，但是发现缓存失效了，暂时注释掉
//    @Cacheable(cacheNames = "orderStatistics", key = "'statistics'")
    public Result<OrderStatisticsVO> statistics() {
        log.info("订单统计");
        OrderStatisticsVO orderStatisticsVO = orderService.getStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 订单详情
     * @param id 订单id
     * @return Result<OrderVO> 订单详情
     */
    @GetMapping("/details/{id}")
    @ApiOperation("订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("订单详情,{}" , id);
        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("确认订单")
    public Result confirm(@RequestBody OrdersConfirmDTO  ordersConfirmDTO) {
        log.info("确认订单,{}" , ordersConfirmDTO);
        orderService.confirmOrder(ordersConfirmDTO);
        return Result.success();
    }


    @PutMapping("/rejection")
    @ApiOperation("订单拒绝")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("订单拒绝,{}" , ordersRejectionDTO);
        orderService.rejectionOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 订单取消
     * @param ordersCancelDTO 取消订单的请求体
     * @return Result 操作结果
     */
    @PutMapping("/cancel")
    @ApiOperation("订单取消")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("订单取消,{}" , ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 订单配送
     * @param id 订单id
     * @return Result 操作结果
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("订单配送")
    public Result delivery(@PathVariable Long id) {
        log.info("订单配送,{}" , id);
        orderService.deliveryOrder(id);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("订单完成")
    public Result complete(@PathVariable Long id) {
        log.info("订单完成,{}" , id);
        orderService.completeOrder(id);
        return Result.success();
    }






}
